﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace PachonDemo
{
  public class Comm
    {
      #region 获取每个地址页面的所有内容
        /// <summary>
        /// 获取每个地址页面的所有内容
        /// </summary>
        /// <param name="webUrl">网址</param>
        /// <param name="endcode">网址的编码格式</param>
        /// <returns></returns>
        public string GetUrlContent(string webUrl, Encoding endcode)
        {
            string rl;
            WebRequest Request = WebRequest.Create(webUrl);
            //Request.Credentials = CredentialCache.DefaultCredentials;
            WebResponse Response = Request.GetResponse();
            Stream resStream = Response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, endcode);//?
            StringBuilder sb = new StringBuilder();
            while ((rl = sr.ReadLine()) != null)
            {
                sb.Append(rl);
            }
            string str = sb.ToString().ToLower();

            // Request.GetResponse().Close();
            return str;
        } 
        #endregion



      #region 判断正则表达式
      /// <summary>
      /// 判断正则表达式
      /// </summary>
      /// <param name="sourceStr">页面html</param>
      /// <param name="regex1"></param>
      /// <param name="regex2"></param>
      /// <param name="regex3"></param>
      /// <param name="inweurl">入口网址,可以为空</param>
      /// <returns></returns>
      public List<string> GetRegexString(string sourceStr, string regex1, string regex2, string regex3, string inweurl=null)
      {
          List<string> regexstr = null;
          if (string.IsNullOrEmpty(sourceStr) == true)
              return null;

          regexstr = new List<string>();
          Regex reg = new Regex(regex1);
          Regex reg2 = new Regex(regex2);
          Regex reg3 = new Regex(regex3);
          var mat = reg.Matches(sourceStr);
          List<string> mc = new List<string>();
          foreach (Match a in mat)
          {
              var aaaa = a.Groups[0].Value;
              if (string.IsNullOrEmpty(regex2) == false)
              {
                  aaaa = reg2.Match(aaaa) + "";
                  if (string.IsNullOrEmpty(regex3) == false)
                  {
                      aaaa = reg3.Match(aaaa) + "";
                  }
              }
              mc.Add(aaaa);
          }
          if (string.IsNullOrEmpty(inweurl) == false)
          {
       
              string url = inweurl;
              foreach (string a in mc)
              {
                  regexstr.Add(url + a);
              }
          }
          else
          {
              foreach (string a in mc)
              {
                  regexstr.Add(a);
              }
          }
          return regexstr;
      }
      
      #endregion


      /// <summary>
      /// 获取GUID表示码
      /// </summary>
      /// <returns></returns>
      public string GetGUIDstringid()
      {
          Guid gui=new Guid ();
       return gui.ToString("N");
      }

      #region 生成单据号 
      /// <summary> 
      /// 生成单据号 
      /// </summary> 
      /// <param name="pFromType"></param> 
      /// <returns></returns>
      public string GetFormCode(FormType pFromType)
      {
          string formcode = "";
          switch (pFromType)
          {
              case FormType.CaiGou:
                  {
                      formcode = "CG";
                      break;
                  }
              case FormType.ChaiXie:
                  {
                      formcode = "CX";
                      break;
                  }
              case FormType.ChuKu:
                  {
                      formcode = "CK";
                      break;
                  }
              case FormType.LingLiao:
                  {
                      formcode = "LL";
                      break;
                  }
              case FormType.PeiChang:
                  {
                      formcode = "PC";
                      break;
                  }
              case FormType.RuKu:
                  {
                      formcode = "RK";
                      break;
                  }
              case FormType.ShengChan:
                  {
                      formcode = "SC";
                      break;
                  }
              case FormType.SunYi:
                  {
                      formcode = "SY";
                      break;
                  }
              case FormType.TuiHuo:
                  {
                      formcode = "TH";
                      break;
                  }
              case FormType.XiaoShou:
                  {
                      formcode = "XS";
                      break;
                  }
              case FormType.PeiSong:
                  {
                      formcode = "PS";
                      break;
                  }
              case FormType.ZengPin:
                  {
                      formcode = "ZP";
                      break;
                  }
              case FormType.FuKuan:
                  {
                      formcode = "FK";
                      break;
                  }
              case FormType.ShouKuan:
                  {
                      formcode = "SK";
                      break;
                  }
              case FormType.pachon:
                  {
                      formcode = "PAC";
                      break;
                  }
          }
          formcode += DateTime.Now.Year.ToString();
          formcode += DateTime.Now.Month.ToString().Length == 1 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString();
          formcode += DateTime.Now.Day.ToString().Length == 1 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString();
          formcode += DateTime.Now.Hour.ToString().Length == 1 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
          formcode += DateTime.Now.Minute.ToString().Length == 1 ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
          formcode += DateTime.Now.Second.ToString().Length == 1 ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
          if (DateTime.Now.Millisecond.ToString().Length == 1)
          {
              formcode += "00" + DateTime.Now.Millisecond.ToString();
          }
          else if (DateTime.Now.Millisecond.ToString().Length == 2)
          {
              formcode += "0" + DateTime.Now.Millisecond.ToString();
          }
          else
          {
              formcode += DateTime.Now.Millisecond.ToString();
          }
          return formcode;
      }
      #endregion

      /// <summary>
      /// 生成随机编码
      /// </summary>
      /// <param name="N"></param>
      /// <returns></returns>
      public string RandCode(int N)
      {
          char[] arrChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
          StringBuilder num = new StringBuilder();
          Random rnd = new Random(Guid.NewGuid().GetHashCode());
          for (int i = 0; i < N; i++)
          {
              num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
          }
          return num.ToString();
      }
      

      public void WriteToLog(string msg)
      {
          string filePath = System.AppDomain.CurrentDomain.BaseDirectory;
          filePath = filePath + "error.log";
          StreamWriter writer = File.AppendText(filePath);
          writer.WriteLine(DateTime.Now.ToString() + " - " + msg);
          writer.Close();
      }


      /// <summary>
      /// 反序列化
      /// </summary>
      /// <param name="type">对象类型</param>
      /// <param name="filename">文件路径</param>
      /// <returns></returns>
      public  object Load(Type type, string filename)
      {
          FileStream fs = null;
          try
          {
              // open the stream...
              fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
              XmlSerializer serializer = new XmlSerializer(type);
              return serializer.Deserialize(fs);
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              if (fs != null)
                  fs.Close();
          }
      }

      /// <summary>
      /// 序列化
      /// </summary>
      /// <param name="obj">对象</param>
      /// <param name="filename">文件路径</param>
      public  void Save(object obj, string filename)
      {
          FileStream fs = null;
          // serialize it...
          try
          {
              fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
              XmlSerializer serializer = new XmlSerializer(obj.GetType());
              serializer.Serialize(fs, obj);
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              if (fs != null)
                  fs.Close();
          }
      }





  }
    public static class baseTime
    {
      /// <summary>
      /// 时间是否为空
      /// </summary>
      /// <param name="dt">时间对象</param>
      /// <returns></returns>
      public static bool DateIsNullorEmpty(object dt)
      {
          if (dt == null || dt is DBNull || dt == DBNull.Value)
              return true;
          else return false;
      }

      /// <summary>
      /// 时间转化
      /// </summary>
      /// <param name="ob"></param>
      /// <returns></returns>
      public static DateTime ConverToDateTime(object ob)
      {
          if (DateIsNullorEmpty(ob) == true)
              return Convert.ToDateTime("0001-01-01");
          else
          return Convert.ToDateTime(ob);
      }
    }
    public enum FormType
    {
        pachon,
        XiaoShou,
        CaiGou,
        ChaiXie,
        ChuKu,
        LingLiao,
        PeiChang,
        RuKu,
        ShengChan,
        SunYi,
        TuiHuo,
        PeiSong,
        ZengPin,
        FuKuan,
        ShouKuan,
    }

    //#region demo
    //  private void demo()
    //  {
    //      //string str = GetUrlContent("http://labour-cn.com/ldhshbzjxxgk/xxgkzd/", Encoding.UTF8);
    //      //Regex reg = new Regex("<a[^>]+href=\"[^\"]+\"[^>]+title=\"[^>]+\">");
    //      //Regex reg2 = new Regex("/\\d[^\\.]+\\.htm");
    //      //var mat = reg.Matches(str);
    //      //List<string> mc = new List<string>();
    //      //foreach (Match a in mat)
    //      //{
    //      //    var aaaa = a.Groups[0].Value;
    //      //    var maat = reg2.Match(aaaa) + "";
    //      //    mc.Add(maat);
    //      //}
    //      //string strrr = "";
    //      //foreach (string a in mc)
    //      //{
    //      //    strrr = strrr + a + "\n";
    //      //}
    //      //richTextBox1.Text = strrr;
    //  } 
    //  #endregion


    //public enum Endcodes
    //{
    //    UTF8,
    //    GB2312,
    //    GBK,
    //    GB18030
    //}
}
